package com.shop2cn.iapi.coupon.testcase.old;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.mongodb.util.JSON;
import com.shop2cn.iapi.coupon.parameter.CouponInfo;
import com.shop2cn.iapi.coupon.parameter.CouponInfos;
import com.shop2cn.iapi.coupon.parameter.GetUserCouponInfosByBatchBean;
import com.shop2cn.iapi.coupon.service.GetUserCouponInfosByBatchCall;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponManageCallService;
import com.ymttest.business.service.CouponShop2cnCallService;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.EnvSetup;
/**
*获取用户指定批次的优惠券信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserCouponInfosByBatch{
	private static GetUserCouponInfosByBatchBean getusercouponinfosbybatchBean;
	private static GetUserCouponInfosByBatchCall getusercouponinfosbybatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户指定批次的优惠券信息");
	}

	@Before
	public void caseUp() {
		getusercouponinfosbybatchBean=new GetUserCouponInfosByBatchBean();
		getusercouponinfosbybatchCall=new GetUserCouponInfosByBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getusercouponinfosbybatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfosByBatch_001() {
		Logger.start(true,"获取用户指定批次的优惠券信息");
		try {
			
			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			
			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			String couponCode = new CouponShop2cnCallService().receiveCouponInfo(batchCode1, userId);

			getusercouponinfosbybatchBean.setUserId(userId);
			getusercouponinfosbybatchBean.setBatchCode(batchCode1);
			getusercouponinfosbybatchBean.setAppId("aas");
			getusercouponinfosbybatchBean.setRequestId("sdgfdgdgfdgf");
			
			getusercouponinfosbybatchCall.setData(getusercouponinfosbybatchBean);
			getusercouponinfosbybatchCall.callService();	
			
	        Logger.verifyEquals("0", getusercouponinfosbybatchCall.getStatusCode1(), "验证返回Code");
	        Logger.verifyEquals("操作成功！",
	        		getusercouponinfosbybatchCall.getMessage(), "验证返回Message");
	        CouponInfos couponInfos = getusercouponinfosbybatchCall.getCouponInfos();
	        
            Map result = (Map) new couponWapper().selectCouponInfoByCouponCode(couponCode).get(0);
	       
           JSONObject obj=new JSONObject(new JSONArray(new JSONObject(getusercouponinfosbybatchCall.getData()).getString("couponInfos")).get(0).toString());
           
	       Logger.comment("验证CouponCode为" + result.get("couponCode") + "记录详细信息");
	       Logger.verifyEquals(result.get("UserId").toString(), obj.getString("userId"), "验证userId与数据库coupon是否一致");
	       Logger.verifyEquals(result.get("BatchCode"), obj.getString("batchCode"), "验证batchCode与数据库couponBatch是否一致");
	       Logger.verifyEquals(result.get("CouponCode"), obj.getString("couponCode"), "验证couponCode与数据库coupon是否一致");
	       if(Boolean.parseBoolean(result.get("IsInvalid").toString())==true)
	       {
	        Logger.verifyEquals(0, obj.getInt("remainUseTimes"), "验证remainUseTimes与数据库couponB是否一致");
	       }else
	       {
	    	   Logger.verifyEquals(1, obj.getInt("remainUseTimes"), "验证remainUseTimes与数据库coupon是否一致");
	       }
	       Logger.verifyEquals(new BigDecimal(result.get("MinOrderValue").toString()).stripTrailingZeros().toString(), obj.getString("orderMinAmount"), "验证orderMinAmount与数据库couponBatch是否一致");
	       Logger.verifyEquals(new BigDecimal(result.get("CouponValue").toString()).stripTrailingZeros().toString(), obj.getString("couponValue"), "验证couponValue与数据库couponValue是否一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfosByBatch_002() {
		Logger.start(true,"获取用户指定批次的优惠券信息-指定批次不存在");
		try {
			
			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));

			getusercouponinfosbybatchBean.setUserId(userId);
			getusercouponinfosbybatchBean.setBatchCode("9999");
			getusercouponinfosbybatchBean.setAppId("aas");
			getusercouponinfosbybatchBean.setRequestId("sdgfdgdgfdgf");
			
			getusercouponinfosbybatchCall.setData(getusercouponinfosbybatchBean);
			getusercouponinfosbybatchCall.callService();
			
			Logger.verifyEquals(null,getusercouponinfosbybatchCall.getCouponInfos().getCouponInfos(), "验证Data为空");
			Logger.verifyEquals(true,getusercouponinfosbybatchCall.getMessage().contains("批次不存在"), "验证message");
			Logger.verifyEquals("200106",getusercouponinfosbybatchCall.getStatusCode1(), "验证statusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfosByBatch_003() {
		Logger.start(true,"获取用户指定批次的优惠券信息-用户不存在");
		try {
			
			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));

           CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			
			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			String couponCode = new CouponShop2cnCallService().receiveCouponInfo(batchCode1, userId);
			
			getusercouponinfosbybatchBean.setUserId(99999);
			getusercouponinfosbybatchBean.setBatchCode(batchCode1);
			getusercouponinfosbybatchBean.setAppId("aas");
			getusercouponinfosbybatchBean.setRequestId("sdgfdgdgfdgf");
			
			getusercouponinfosbybatchCall.setData(getusercouponinfosbybatchBean);
			getusercouponinfosbybatchCall.callService();
			
	        Logger.verifyEquals("0", getusercouponinfosbybatchCall.getStatusCode1(), "验证返回statusCode");
	        Logger.verifyEquals("操作成功！",
	        		getusercouponinfosbybatchCall.getMessage(), "验证返回Message");
	        JSONArray obj=new JSONArray(new JSONObject(getusercouponinfosbybatchCall.getData()).getString("couponInfos"));
	        
			Logger.verifyEquals(0,obj.length(), "验证couponInfos为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_GetUserCouponInfosByBatch_004() {
//		Logger.start(true,"获取用户指定批次的优惠券信息-批次存在，优惠券不存在");
//		try {
//			
//			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
//			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
//			
//			// 商家优惠券
//			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
//			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			String couponCode = new CouponShop2cnCallService().receiveCouponInfo(batchCode1, userId);
//			
//			couponbatch batch=new couponbatchWapper().selectBybatchCode(batchCode1).get(0);
//            new couponWapper().updateBatchIdByCouponCode(batch.getBatchId()+22, couponCode);
//			getusercouponinfosbybatchBean.setUserId(userId);
//			getusercouponinfosbybatchBean.setBatchCode(batchCode1);
//			getusercouponinfosbybatchBean.setAppId("aas");
//			getusercouponinfosbybatchBean.setRequestId("sdgfdgdgfdgf");
//			
//			getusercouponinfosbybatchCall.setData(getusercouponinfosbybatchBean);
//			getusercouponinfosbybatchCall.callService();	
//			
//
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}